'use strict'

define([ 'jquery', 'bootstrap', 'select2', 'bodyform', 'shiftform', 'validate',
    'validate.en'], function($) {
  return {
    init : function() {
      this.initPage();
      this.bindClickLanguage();
      this.initCommentEdit();
      this.initRecommendationEdit();
      this.initEffectiveEdit();
      this.initSubmit();
    },
    initPage : function() {
      var that = this;
//      var urlString = window.location.search.split("=")[1]
//      if(urlString == 'en_US') {
//        $(".en-tab").addClass("active")
//        $(".cn-tab").removeClass("active")
//        $(".comment-en").show();
//        $(".comment-cn").hide();
//        var href = $('.edit-readiness').attr("href");
//        href = href + "?language=en";
//        $('.edit-readiness').attr("href", href);
//      } else if(urlString == 'zh_CN') {
//        $(".cn-tab").addClass("active")
//        $(".en-tab").removeClass("active")
//        $(".comment-en").hide();
//        $(".comment-cn").show();
//        var href = $('.edit-readiness').attr("href");
//        href = href + "?language=cn";
//        $('.edit-readiness').attr("href", href);
//      }
      
      /*$(document).on('shown.bs.modal', '.modal', function(){
        if(urlString == 'en_US') {
          $('[name="commentEn"]').show();
          $('[name="commentCn"]').hide();
        } else {
          $('[name="commentEn"]').hide();
          $('[name="commentCn"]').show();
        }
      })*/
    
    },
    
    bindClickLanguage: function() {
//      $(document).on("click", '.language-tab .btn-link', function() {
//        $(this).addClass("active").siblings().removeClass("active")
//        $(".comment-content").hide();
//        $(".comment-"+ $(this).data('language')).show();
//        var href = $('.edit-readiness').attr("href");
//        var temp = href.split("?");
//        href = temp[0] + "?language=" + $(this).data('language');
//        $('.edit-readiness').attr("href", href);
//      })
    },
    
    initCommentEdit: function() {
      $(document).on('shown.bs.modal', '#editDevelopmentRecommendations', function(e){

            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                var chooseValue = "";
                $('.suggest-choose:checked').each(function() {
                	if($(this).val()) {
                		if(!chooseValue) {
                			chooseValue = chooseValue + $(this).val();
                		} else {
                			chooseValue = chooseValue + "," + $(this).val();
                		}
                	}
                })
                $('input[name="chooseValue"]').val(chooseValue);
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
      })
    },
    
    initRecommendationEdit: function() {
      $(document).on('shown.bs.modal', '#editReadinessRecommendation', function(e){

          $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
          var $form = $(e.currentTarget).find("form")                          //bind shiftform
          $form.shiftform({
            beforeSubmit : function(el) {
              $form.trigger("validate.ajax.submit");
              return $.bt_validate.result[$.bt_validate.form_id]
            }
          })
      })
    },
    
    initEffectiveEdit: function() {
        $(document).on('shown.bs.modal', '#editEffective', function(e){

            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        })
      },
    
    initSubmit: function() {
      $(document).on('shown.bs.modal', '#submit', function(e){

          $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
          var $form = $(e.currentTarget).find("form")                          //bind shiftform
          $form.shiftform({
            beforeSubmit : function(el) {
              $form.trigger("validate.ajax.submit");
              return $.bt_validate.result[$.bt_validate.form_id]
            }
          })
      })
    }
    
  }
})
